package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.vo.Like;
import com.ruoyi.system.service.LikeService;
import com.ruoyi.system.service.RedisKeyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Api(tags="点赞信息管理")
@Anonymous
@RestController
@RequestMapping("/system/Like")
public class LikeController extends BaseController {
    @Resource
    private LikeService likeService;
//    @RequestMapping(path = "/like", method = RequestMethod.POST)
    @ApiOperation("点赞阅读分享增加")
    @PostMapping
    public AjaxResult like(Like like) {
        String entityLikeKey="";
        if(like.getType().equals("1")) {
            entityLikeKey = RedisKeyUtil.getEntityREADKey(like.getEntityType(), like.getEntityId());
        } else if (like.getType().equals("2")) {
            entityLikeKey = RedisKeyUtil.getEntityLikeKey(like.getEntityType(), like.getEntityId());
        }else if (like.getType().equals("3")) {
            entityLikeKey = RedisKeyUtil.getEntitySHAREKey(like.getEntityType(), like.getEntityId());
        }
        String userId = like.getUserCode();
        // 点赞
        likeService.like(userId, entityLikeKey);
        // 重新计算数量
        long likeCount = likeService.findEntityLikeCount(entityLikeKey);
        // 重新计算状态
        int likeStatus = likeService.findEntityLikeStatus(userId, entityLikeKey);
        Map<String, Object> map = new HashMap<>();
        map.put("likeCount", likeCount);
        map.put("likeStatus", likeStatus);
        return toAjax(200);
    }
}
